package com.study.graph.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.graph.model.FollowEdge;
import com.study.graph.model.PlayerVertex;
import com.study.graph.repository.PersonRepository;
import com.study.graph.service.IGraphService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class GraphServiceImpl implements IGraphService {

    @Autowired
    private PersonRepository personRepository;

    public boolean addEdges(String edgesfile, String relationship) {
        try {
            FollowEdge follow = new FollowEdge();
            boolean bool = personRepository.insertEdges(follow, relationship);

        } catch (Exception e) {
            log.error("批量增边错误：" + e.getMessage() + "\n");
            throw e;
        }
        return false;
    }

    public String addVertex(PlayerVertex player) {
        boolean bool = personRepository.insertVertex(player);
        return "success";
    }

    /**
     * 删除节点通过id
     */
    public String deleteVertex(PlayerVertex player) {

        return "success";
    }

    /**
     * 新增边
     */
    public String addEdge(int degree, String leftId, String rightId) {
        FollowEdge edge = new FollowEdge();
        edge.setDegree(10);
        try {
            boolean bool = personRepository.insertEdge(edge, leftId, rightId);
            return "success";
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return "failed";
        }
    }

    /**
     * 删除边
     */
    public String deleteEdge(FollowEdge follow) {

        return "success";
    }


    /**
     * 新增边和终点
     */
    public String addNodeEdge(PlayerVertex vertex, FollowEdge edge) throws Exception {
//        String vertexId = graphBuzi.addVertex(vertex);
//        vertex.setId(vertexId);
//        indexBuzi.addOrUpdateIndex(vertex);
//        edge.setRelationShip(DatabaseConfiguration.getRelationshipByLabel(vertex.getLabel()));
//        edge.setTo(vertexId);
//        String edgeId = graphBuzi.addEdges(edge);
//        edge.setId(edgeId);
//        indexBuzi.addOrUpdateIndex(edge);
        return "success";
    }


    /**
     * 通过名称查询
     */
    public String exactMatchQuery(String name, int depth) {
//        List<Path> paths = null;
//        try {
//            paths = graphBuzi.bfExtersion(vertex, depth);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        ResultBuilder resultBuilder = new ResultBuilder();
//        JSONObject result = resultBuilder.graphResult(paths);
//        resultBuilder = null;
//        return result.toString();
        return null;
    }

    /**
     * 获取结果集合中边的名称列表
     */
    private JSONObject getRelationshipName(JSONObject result) {
        if (result.containsKey("nodes")) {
            JSONArray jsonArray = result.getJSONArray("nodes");
            Map maps = new HashMap<Integer, JSONObject>();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String type = jsonObject.getString("type");
                String name = null;
                if ("com".equals(type)) {
                    name = "公司名";
                } else if ("attribute".equals(type)) {
                    name = "属性";
                } else {
                    name = jsonObject.getString("name");
                }
                JSONObject typeobj = new JSONObject();
                typeobj.put("type_en", type);
                typeobj.put("type_cn", type);
                if (!maps.containsKey(typeobj.toString().hashCode())) {
                    maps.put(typeobj.toString().hashCode(), typeobj);
                }
            }
            JSONArray results = new JSONArray();
            for (Object value : maps.values()) {
                //  System.out.println("Value = " + value);
                results.add(value);
            }
            result.put("nodeTypeName", results);
        }
        return result;
    }

    /**
     * 无向搜索
     *
     * @param name
     * @param graphNames
     * @return
     */
//    public String directedBfExtersion(String name, JSONArray graphNames, int depth) {
//        ResultBuilder resultBuilder = new ResultBuilder();
//        JSONObject result = new JSONObject();
//        for (int i = 0; i < graphNames.size(); i++) {
//            String graphName = graphNames.getString(i);
//            try {
//                Graph graph = DatabaseConfiguration.getGraph(graphName);
//                String label = graph.getLabel();
//                Vertex vertex = new Vertex();
//                vertex.setLabel(label);
//                vertex.setName(name);
//                List<Path> paths = null;
//                try {
//                    paths = graphBuzi.directedBfExtersion(vertex, depth);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                JSONObject object = resultBuilder.graphResult(paths);
//                result = resultBuilder.mergeResult(result, object);
//
//
//                paths = graphBuzi.bfExtersion(vertex, depth);
//
//                object = resultBuilder.graphResult(paths);
//                result = resultBuilder.mergeResult(result, object);
//
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        result = resultBuilder.reDupResult(result);
//        result = getRelationshipName(result);
//        return result.toString();
//    }


    /**
     * 根据精准name查图
     *
     */
//    public String exactMatchQuery(String name, JSONArray graphNames, int depth) {
//        ResultBuilder resultBuilder = new ResultBuilder();
//        JSONObject result = new JSONObject();
//        for (int i = 0; i < graphNames.size(); i++) {
//            String graphName = graphNames.getString(i);
//            try {
//                Graph graph = DatabaseConfiguration.getGraph(graphName);
//                String label = graph.getLabel();
//                Vertex vertex = new Vertex();
//                vertex.setLabel(label);
//                vertex.setName(name);
//                List<Path> paths = null;
//                try {
//                    paths = graphBuzi.bfExtersion(vertex, depth);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                JSONObject object = resultBuilder.graphResult(paths);
//                result = resultBuilder.mergeResult(result, object);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        result = resultBuilder.reDupResult(result);
//        result = getRelationshipName(result);
//        return result.toString();
//    }

    /**
     * 索引匹配查询
     *
     * @param keyword
     * @return
     */
    public String indexMatchingQuery(String keyword, JSONArray graphNames) {

        return null;
    }


    /**
     * 根据 id与深度返回结果
     *
     * @param id
     * @param depth
     * @return
     */
    public String getGraphById(Long id, int depth) {
//        List<Path> paths = null;
//        try {
//            paths = graphBuzi.checkGraphById(id, depth);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        ResultBuilder resultBuilder = new ResultBuilder();
//        JSONObject result = resultBuilder.graphResult(paths);
//        result = getRelationshipName(result);
//        resultBuilder = null;
//        return result.toString();
        return null;
    }

}
